import {login, logout,} from '@/api/user'
import {getUserInfo, saveUserInfo} from '@/api/sys-user'
import {getFileById, getMessageData, replacePhoneNum, seenMessage, sendReplaceMsg} from '@/api/data'
import {getToken, setToken} from '@/libs/util'

export default {
  state: {
    userName: '',
    userId: '',
    avatarImgPath: '',
    token: getToken(),
    access: '',
    hasGetInfo: false,
    requestInfo:true,
    redis:{
      userMap:[],
      roleMap:[],
    },
    msgData:[]
  },
  mutations: {
    setAvatar (state, avatorPath) {
      state.avatarImgPath = avatorPath
    },
    setUserId (state, id) {
      state.userId = id
    },
    setUserName (state, name) {
      state.userName = name
    },
    setAccess (state, access) {
      state.access = access
    },
    setToken (state, token) {
      state.token = token
      setToken(token)
    },
    setHasGetInfo (state, status) {
      state.hasGetInfo = status
    },
    setRequestInfo (state, status) {
      state.requestInfo = status
    },
    setRedis (state, redis) {
      state.redis = Object.assign(state.redis,redis)
    },
    setMsgData (state, data) {
      if (data instanceof Array){
        state.msgData = data
      }else{
        state.msgData = state.msgData.reverse().concat(data).reverse()
      }
    },
  },
  actions: {
    // 登录
    handleLogin ({ commit }, { userName, password }) {
      userName = userName.trim()
      return new Promise((resolve, reject) => {
        login({
          userName,
          password
        }).then(res => {
          if (res.code==='0'){
            commit('setRequestInfo', true)
            const data = res.data
            commit('setToken', data.token)
            sessionStorage.setItem("token",data.token)
            resolve(res)
          }else {
            commit('setRequestInfo', false)
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 退出登录
    handleLogOut ({ state, commit }) {
      return new Promise((resolve, reject) => {
        logout({}).then(res => {
          if (res.code==='0'){
            state.user = null
            commit('setToken', '')
            commit('setRedis', {})
            commit('setMsgData', [])
            commit('setAccess', [])
            commit('setHasGetInfo', false)
            sessionStorage.clear()
            resolve()
            console.log(state.userName+'已断开webSocket连接')
          }
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 获取用户相关信息
    getUserInfo ({ state, commit }) {
      return new Promise((resolve, reject) => {
        try {
          getUserInfo().then(res => {
            const data = res.data
            commit('setAvatar', data.avatar)
            commit('setUserName', data.name)
            commit('setUserId', data.id)
            commit('setAccess', ["lc"])
            commit('setHasGetInfo', true)
            resolve(data)
          }).catch(err => {
            reject(err)
          })
        } catch (error) {
          reject(error)
        }
      })
    },

    getMsgData(){
      return new Promise((resolve, reject) => {
        try {
          getMessageData().then(res=>{
            resolve(res)
          }).catch(err => {
            reject(err)
          })
        } catch (error) {
          reject(error)
        }
      })

    },

    seenMessage({ commit }, { id}){
      return new Promise((resolve, reject) => {
        try {
          seenMessage({ id }).then(res=>{
            resolve(res)
          }).catch(err => {
            reject(err)
          })
        } catch (error) {
          reject(error)
        }
      })

    },
    replacePhoneNum({ commit }, data){
      return new Promise((resolve, reject) => {
        try {
          replacePhoneNum(data).then(res=>{
            resolve(res)
          }).catch(err => {
            reject(err)
          })
        } catch (error) {
          reject(error)
        }
      })

    },
    sendReplaceMsg({ commit }, data){
      return new Promise((resolve, reject) => {
        try {
          sendReplaceMsg(data).then(res=>{
            resolve(res)
          }).catch(err => {
            reject(err)
          })
        } catch (error) {
          reject(error)
        }
      })
    },
    saveUserInfo({ commit }, data){
      return new Promise((resolve, reject) => {
        try {
          saveUserInfo(data).then(res=>{
            resolve(res)
          }).catch(err => {
            reject(err)
          })
        } catch (error) {
          reject(error)
        }
      })
    },

    getImgUrl({ commit }, params){
      return new Promise((resolve, reject) => {
        try {
          getFileById(params).then(res=>{
            resolve(res)
          }).catch(err => {
            reject(err)
          })
        } catch (error) {
          reject(error)
        }
      })
    },
  }
}
